package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.form.SearchForm;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2020-11-10
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Page<Teacher> pageByCondition(SearchForm searchForm, Long pageNum, Long size) {
//        判断条件是否存在
        Page<Teacher> page = new Page<>(pageNum, size);
        if (searchForm==null){
//            查询所有的数据的分页数据
           page = baseMapper.selectPage(page, null);
           return page;
        }

//        带条件查询数据后在查询分页数据
        String joinDateBegin = searchForm.getJoinDateBegin();
        String joinDateEnd = searchForm.getJoinDateEnd();
        Integer level = searchForm.getLevel();
        String name = searchForm.getName();
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
//            如果我们输入"周",我们希望查询到以周开始的所有名称
//            %加在右边不会影响索引
            queryWrapper.likeRight("name",name);
        }

        if (level!=null){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge("join_date",joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le("join_date",joinDateEnd);
        }

        page = baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight("name",key);
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);//返回值是Map列表
        System.out.println("===============关键字查询===TeacherServiceImpl======");
        return list;
    }
}
